In this video you will learn how to merge time series and real world it is often the case that yes for
example historical stock prices for Apple and one data frame and prices for Boeing in another data frame.
And typically the periods are the time stamps are different for both data frames and then you want to
join both data frames into one data frame.
And even if the Axis advance the panda's methods to join and merge data frames.
That's a pretty pragmatic and straightforward way to do this with the basic tools that we have already
learned and the first of all we import pandas then we import the CSP five stocks and we only select
the closing prices.
So here we have our six stocks and then let's assume that we have Apple stock prices then one data frame
for the period to tend to 14.
So that's the apple data frame.
And uh then in a separate data frame we have stock prices for Boeing for 2 12 to 16.
So that's uh the Boeing data frame.
And let's assume that we want to work with the apple prices and Boeing prices and compare them and work
with them together and we can actually simply do this by creating for example in our apple data frame
a new column Boeing and we assign uh the Boeing stock prices or let's do this here and have a look and
here to 10 we are starting with missing values because our Boeing data frame only starts in 2012 and
it's also if I look here at the last timestamps in 214 and here we have the Boeing prices the to 14
and actually even copying here the Boeing prices into our Apple data frame so penned us automatically
aligns the the prices here corresponding to the timestamps.
So each and every price here will be at the identical timestamp here and the apple data frame than before
in the Boeing data frame.
So this works pretty well and there is actually nothing we have to care about and there's actually no
need for specialized the merging or joining methods.
So that's uh pretty cool and simple here.
And of course if we are only interested in the timestamps uh where we have uh data for Apple and for
Boeing then we can drop here the rows with at least the one missing value with the drop and a method.
So here we have the data from 212 to 214 and actually both of the re indexed method.
We can also align the Boeing data frame on the timestamps that we have in the Apple data frame so we
use the array index method on the Boeing data frame and pass the day time index of the apple data frame.
And if we get actually the same result.
So we have the timestamps that we have ended up with data frame and the corresponding Boeing prices
and let's assume we have a set time series with historical stock prices for this.
And we have them for the period from 210 to 216 and we have actually a weekly date of this Fridays so
let's have a look at the distance data frame from 2010 to 2016 and the weekly price data and let's recall
our Apple data frame.
So here we have daily data.
And also here we can't read the additional column with the Disney prices and we can assign.
The Disney column off the Disney radar frame.
So here we have Disney prices but only for Fridays and otherwise we have missing values and also here
we can re index the Disney data frame for the day time index that we have in the Apple data frame and
also here we have only prices soar values on Fridays and typically when merging time series serve as
a different frequency.
It definitely makes sense to align the data based on the time series with a lower frequency.
So here there is no time series and therefore we can also create here the additional column appear with
the the apple prices and we have actually our weekly daytime index with the weekly prices for Disney
and for Apple.
So also here the apple prices that we copied from the Apple data frame are automatically aligned to
the daytime index.
And what this new data frame and that's actually nothing to care or two very here.
So this works pretty well and automatically.
And with this we are finished here and I hope to see you also in the next video by.